<template>
	<view>
		<uni-swipe-action>
			<uni-swipe-action-item v-for="address in addresses" :right-options="options" @click="onOptionsClick($event, index)">
				<view class="bg-white px-2 py-2 border-bottomo" @click="onAddressClick">
					<view class="font text-dark">
						<text class="">{{address.realname}}</text>
						<text class="ml-2">{{addresses.mobile}}</text>
					</view>
					<view class="font-sm text-grey mt-2">
						{{address.region+address.detail}}
					</view>
				</view>
			</uni-swipe-action-item>

		</uni-swipe-action>
		<view class="mt-2 text-center">
			
			<button type="warn" class="border" size="mini" plain @click="onAddAddressClick">
				<uni-icons
					type="plusempty"
					color="warn"
					size="18"
				/>
				新增地址
			</button>
		</view>
	</view>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue'
  import {onLoad} from '@dcloudio/uni-app'
  import userAddressStore from "@/stores/address"
  import userHttp from "../../apis/user/userHttp";
  import UniSwipeAction from "../../uni_modules/uni-swipe-action/components/uni-swipe-action/uni-swipe-action.vue";
  const addressStore = userAddressStore()
  let addresses = ref([])
  let swipeAction = ref();
  const FromEnum = {
    seckill: 1,
    mine: 2
  }
  let currentAddress = {
      id: "1",
      realname:"李四",
      mobile:"123456789",
      region:"北京市 朝阳区 朝外街道 朝外小区 1号楼 101单元 101室",
      detail:"12345231 "
    }
  let from = reactive(FromEnum.mine)
  onLoad((options) => {
    uni.$on("address-edit", (params)=>{
      console.log('params')
    })
    uni.$on("address-add", (params)=>{
      console.log(params)
    })
    // from ='抢购Seckill'， from='我的mine'
    if(options.from === 'seckill'){
      from = FromEnum.seckill
    }
  })
  onMounted(async () => {
    const result = await userHttp.getAddressList();
    addresses.value = result.addresses;
  })

  let options = ref([
    {
      text: '编辑',
      style: {
        backgroundColor: '#2d78a6'
      },
      methods: onAddressClick
    },
    {
      text: '删除',
      style: {
        backgroundColor: '#ff5a5f'
      }
    }
  ])
  function onOptionsClick(e, index) {
    swipeAction.value.closeAll();

    let address = addresses.value[index];
    // 1. 编辑
    if(e.index == 0){
      addressStore.address = address;
      addressStore.address = {
        id: "1",
        realname: "李四",
        telephone: "18899990000",
        region: '湖南省长沙市芙蓉区',
        detail: "晚报大厦"
      }
      uni.navigateTo({
        url: "/pages/address-edit/address-edit"
      })
    }else{
      // 弹出对话框
      uni.showModal({
        title: "提示",
        content: "您确定要删除此地址吗？",
        success: async () => {
          await userHttp.deleteAddress(address.id)
          addresses.value.splice(index, 1);
        }
      })
    }
  }
  function onAddressClick(index) {
    if (from === FromEnum.seckill) {
      addressStore.address = currentAddress
      uni.$emit('choose-address', {'address': currentAddress})
      uni.navigateBack()
      }
    else{
      uni.navigateTo({ url: '/pages/address-edit/address-edit' })
    }
    }

  const onAddAddressClick = () => {
    addressStore.clearAddress()
    uni.navigateTo({ url: '/pages/address-edit/address-edit' })
  }
</script>

<style></style>
